import { request } from '../../request/index.js'
import { requestPayment, showToast } from '../../utils/asyncWx.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderStatus: '',
    groupTel: true,
    groupLeader: {},
    orderInfo: {}
  },
  header: {
    token: ""
  },
  orderId: '',
  //立即支付
  async handlePay() {
    try {
      const orderId = this.orderId
      //const orderPaymentMethod = 1
      const header = this.header
      const params = { orderPaymentMethod: 1, orderId }
      const res = await request({ url: '/order/fresh/' + orderId + '/payment', method: 'PUT', data: params, header })
      const paymentResult = res.data

      const pay = {
        nonceStr: paymentResult.nonceStr,
        package: paymentResult.packageValue,
        paySign: paymentResult.paySign,
        signType: paymentResult.signType,
        timeStamp: paymentResult.timeStamp
      }

      await requestPayment(pay)

      await showToast({ title: '支付成功' })
      wx.navigateTo({
        url: '../fruit-order/index?type=3'
      });
    } catch (error) {
      await showToast({ title: '订单已取消' })
      // let pages = getCurrentPages();
      // let prevPage = pages[pages.length - 2]
      // prevPage.setData({
      //   type: 1
      // })
      wx.navigateTo({
        url: '../fruit-order/index?type=1'
      });
      console.log(error)
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const token = wx.getStorageSync("token");
    this.header.token = token
    // 设置标题
    wx.setNavigationBarTitle({
      title: options.orderStatus,
    })
    this.setData({
      orderStatus: options.orderStatus
    })
    const { orderId } = options
    this.orderId = orderId
    this.getOrderStatus(orderId)
    this.getOrderAddress(orderId)
    this.getGroupLeader(orderId)
    this.getGoodsList(orderId)
  },
  //发起订单状态查询请求
  async getOrderStatus(orderId) {
    const token = wx.getStorageSync("token");
    const header = { token }
    const res = await request({ url: '/order/' + orderId, data: { orderId }, header })
    this.setData({
      orderInfo: res.data
    })
  },
  //发起订单地址信息请求
  async getOrderAddress(orderId) {
    const header = this.header
    const res = await request({ url: '/order/' + orderId + '/address', data: { orderId }, header })
    console.log(res)
  },
  //获取订单团长信息
  async getGroupLeader(orderId) {
    const header = this.header
    const res = await request({ url: '/order/' + orderId + '/groupLeader', data: { orderId }, header })
    this.setData({
      groupLeader: res.data
    })
  },
  //获取商品信息
  async getGoodsList(orderId) {
    const header = this.header
    const res = await request({ url: '/order/' + orderId + '/goods', data: { orderId }, header })
    console.log(res)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})